package com.itheima.web.controller;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //【第一步】判断是否请求是否支持文件上传,目的是判断表单是否有enctype="multipart/form-data"
        if(ServletFileUpload.isMultipartContent(request)){
            //【第二步】创建磁盘工厂对象，用于将页面上传的文件保存到硬盘中。
            DiskFileItemFactory factory=new DiskFileItemFactory();
            //【第三步】创建ServletFileUpload文件上传核心对象
            ServletFileUpload fileUpload=new ServletFileUpload(factory);
            //【第四步】解析请求对象，获取表单项集合
            try {
                List<FileItem> fileItems = fileUpload.parseRequest(request);
                //【第五步】遍历集合，判断是普通表单项还是文件上传项，如果是文件上传项就将文件写到服务器硬盘中。
                fileItems.forEach(fileItem -> {
                    //判断是普通表单项还是文件上传项
                    if(fileItem.isFormField()){
                        //普通表单项，就是普通输入框
                        //获取name属性值
                        String fieldName = fileItem.getFieldName();
                        //获取value值,字符集解决中文乱码问题
                        try {
                            String value = fileItem.getString("utf-8");
                            System.out.println(fieldName+"="+value);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }else{
                        //文件上传项
                        //获取文件名
                        String filename = fileItem.getName();
                        System.out.println("filename = " + filename);
                        //获取文件在服务器中保存的位置
                        String realPath = getServletContext().getRealPath("/upload");
                        //将文件写到这个目录中
                        try {
                            fileItem.write(new File(realPath,filename));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (FileUploadException e) {
                e.printStackTrace();
            }

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
